home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / nShell Prog / delay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-23  |  2.5 KB  |  116 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     delay.c 
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. #define TICKS_PER_SEC    60
  16. #define SECS_PER_MINUTE    60
  17.  
  18. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  19. #include <A4Stuff.h>
  20. #endif
  21.  
  22. #include "nshc.h"
  23.  
  24. #include "arg_utl.proto.h"
  25. #include "nshc_utl.proto.h"
  26.  
  27. // prototypes - for local use only
  28.  
  29. void delay_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  30. void delay_continue( t_nshc_parms *nshc_parms );
  31. void delay_stop( t_nshc_parms *nshc_parms );
  32.  
  33. // state machine
  34.  
  35. void delay_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  36. {
  37.     int        tArg;        // position in arg list of delay time
  38.     long    dTime;        // delay in seconds
  39.     Handle    hTime;        // handle to hold target time
  40.     
  41.     if ( tArg = nshc_next_operand( nshc_parms, 0 ) )
  42.         if ( nshc_is_numeric_operand( nshc_parms, tArg ) ) {
  43.  
  44.             dTime = arg_to_int( nshc_parms, tArg ) * TICKS_PER_SEC;
  45.             
  46.             if (nshc_got_option( nshc_parms, 'm' ))
  47.                 dTime *= SECS_PER_MINUTE;
  48.  
  49.             hTime = NewHandle(sizeof(long));
  50.     
  51.             if (hTime) {
  52.                 **(long **)hTime = TickCount() + dTime;
  53.                 nshc_parms->data = hTime;
  54.                 nshc_parms->action = nsh_continue;
  55.                 }
  56.             else {
  57.                 nshc_calls->NSH_putStr_err("\pdelay: Could not allocate storage.\r");
  58.                 nshc_parms->action = nsh_idle;
  59.                 nshc_parms->result = NSHC_ERR_MEMORY;
  60.                 }
  61.                 
  62.             return;
  63.             
  64.             }
  65.  
  66.     nshc_calls->NSH_putStr_err("\pUsage: delay time [-m].\r");
  67.     nshc_parms->result = NSHC_ERR_PARMS;
  68.     nshc_parms->action = nsh_idle;
  69. }
  70.  
  71. void delay_continue( t_nshc_parms *nshc_parms )
  72. {
  73.     long time;
  74.     
  75.     time = TickCount();
  76.     
  77.     if (time > **(long **)nshc_parms->data)
  78.         delay_stop(nshc_parms);        
  79. }
  80.  
  81. void delay_stop( t_nshc_parms *nshc_parms )
  82. {
  83.     if (nshc_parms->data)
  84.         DisposeHandle(nshc_parms->data);
  85.         
  86.     nshc_parms->result = NSHC_NO_ERR;
  87.     nshc_parms->action = nsh_idle;
  88. }
  89.  
  90. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  91. {
  92. #ifdef __MWERKS__
  93.     long oldA4  = SetCurrentA4();
  94. #endif
  95.     
  96.     if ( !nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ) ) {
  97.         
  98.         switch (nshc_parms->action) {
  99.             case nsh_start:
  100.                 delay_start(nshc_parms,nshc_calls);
  101.                 break;
  102.             case nsh_continue:
  103.                 delay_continue(nshc_parms);
  104.                 break;
  105.             default:
  106.                 delay_stop(nshc_parms);
  107.                 break;
  108.             }
  109.             
  110.         }
  111.  
  112. #ifdef __MWERKS__
  113.     SetA4(oldA4);
  114. #endif
  115. }
  116.